package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.entity.ServiceOrder;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.ServiceOrderService;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author：fuchunyu
 * Date：2017/10/27
 * Description：电子保修卡
 */
@Controller
@RequestMapping("/serviceOrderC")
public class ServiceOrderController {
    @Resource
    private UserService userService;
    @Resource
    private ServiceOrderService serviceOrderService;

    @ResponseBody
    @RequestMapping(value = "searchService.json",produces = "application/json;charset=utf-8")
    public String searchService(String companyCode,ModelMap model, String token, String page){
        User user=userService.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        PageView pageview =new  PageView();
        int pageNo = 1;
        int pageSize=5;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }
        ServiceOrder serviceOrder=new ServiceOrder();
        serviceOrder.setUserId(user.getId());
        serviceOrder.setState(true);
        pageview=serviceOrderService.getPage(pageNo,pageSize,serviceOrder);

        if (pageview!=null){
            model.put("data",serviceOrderService.convert(pageview.getRecords()));
            model.put("recordsTotal", pageview.getRowCount());
            model.put("totalPage", pageview.getPageCount());
            model.put("pageNo", page);
        }
        return CodeContent.returnSuccess(model);
    }
}
